<?
layout("/inc/layout.html",{title:sysTitle}){
?>

<div id="page-wrapper">
      <!--BEGIN TITLE & BREADCRUMB PAGE-->
      <div id="title-breadcrumb-option-demo" class="page-title-breadcrumb">
          <div class="page-header pull-left">
              <div class="page-title">分布式任务调度</div>
          </div>
          <ol class="breadcrumb page-breadcrumb pull-left">
              <li><i class="fa fa-home"></i>&nbsp;<a href="main.html">首页</a>&nbsp;&nbsp;<i class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>
              <li>分布式任务调度&nbsp;&nbsp;<i class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>
              <li class="active">调度日志</li>
          </ol>
           
          <div class="clearfix"></div>
      </div>
      <!--END TITLE & BREADCRUMB PAGE-->
      <!--BEGIN CONTENT-->
      <div class="page-content">
      <div class="row mbl">
      		<div class="col-sm-3">
				<div class="input-group">
              		<span class="input-group-addon">执行器</span>
             			<select class="form-control" id="jobGroup"  >
                		</select>
            	</div>
	        </div>
      		<div class="col-sm-3">
				<div class="input-group">
	                <span class="input-group-addon">任务</span>
                    <select class="form-control" id="jobId"  >
		   			</select>
	            </div>
	        </div>
          <div class="col-sm-4">
				<div class="input-group">
					<span class="input-group-addon">调度时间</span>
					<div class="btn btn-blue reportrange">
						<i class="fa fa-calendar"></i>&nbsp;<span></span>&nbsp;&nbsp;
						<i class="fa fa-angle-down" style="margin-top: 100xp;"></i> 
						<input type="hidden" name="datestart" id="datestart"/>
						<input type="hidden" name="endstart" id="endstart"/>
				   </div>
               		<span class="input-group-btn"><button type="button" data-toggle="dropdown" class="btn btn-success dropdown-toggle" onclick="list1()">&nbsp;&nbsp;搜&nbsp;索&nbsp;&nbsp; </button></span>
				</div>
			</div>
		</div>
		<div calss="row mbl">
           <div class="col-sm-12">
          		 <div class="tb-group-actions pull-right"><span>当前共<label id="pageSize"></label>条记录:</span>
                   <select id="pageNumber" class="table-group-action-input form-control input-inline input-small input-sm mlm" onchange="list1()">
                       <option value="5">5</option>
                       <option value="10" >10</option>
                       <option value="15" selected>15</option>
                       <option value="25">25</option>
                       <option value="50">50</option>
                   </select>&nbsp;
               <div class="tb-group-actions pull-right">
                   </div>
               </div>
           </div>
       </div>
 
      <table class="table table-hover table-striped table-bordered table-advanced tablesorter mbn">
            <thead>
                <tr>                 
                    <th >调度时间</th>
                    <th >调度结果</th>
                    <th >调度备注</th>
                    <th >执行器地址</th>
                    <th >JobHandler</th>
                    <th >任务参数</th>
                    <th >执行时间</th>
                    <th >执行结果</th>
                    <th >执行备注</th>
                    <th >可操作</th>
                </tr>
            </thead>
            <tbody id="newspaperls"></tbody>
        </table>
		<div id="dataNull" class="panel-body"
				style="height: 90px; line-height: 50px;" align="center">
				<span class='subtitle' style="font-size: 15px;">数据为空!</span>
		</div>
        <div id="page1" style="text-align: center; margin-top: 20px;"></div>
    </div>
                     
    <!--END CONTENT-->
    </div>

  
<?} ?>
<script>
var jobinfoiid=${jobinfoiid};
var jobgroupiid=${jobgroupiid};
var cur;var id;
$(function(){
	init();
	$('.reportrange span').html(moment().format('YYYY-MM-DD 00:00:00') + ' - ' + moment().format('YYYY-MM-DD HH:mm:ss'));
	$('input[name="datestart"]').val(moment().format('YYYY-MM-DD 00:00:00'));
	$('input[name="endstart"]').val(moment().format('YYYY-MM-DD HH:mm:ss'));
    cur=1;
	list1(cur);
	
});
function get(){
  list1(cur);
}
function getlay() {  
    return id;
}
function list1(curr) {
	$.getJSON("./xxljob_log_pageList.do",
	{
		page : curr || 1,
		pageNumber:$("#pageNumber").val(),
		jobGroup:$("#jobGroup option:selected").val(),
		jobId:$("#jobId option:selected").val(),
		datestart:$("#datestart").val(),
		endstart:$("#endstart").val(),
	},
	function(res) {
	//此处仅仅是为了演示变化的内容
		var str = "";
		if(res.listsize>0){
			$("#dataNull").hide();
		}else{
			$("#dataNull").show();
		}
		$("#newspaperls").html("")
		for ( var i = 0; i < res.listsize; i++) {
			str += "<tr><td> <label >"
					+  (res.result[i].triggerTime?moment(new Date(res.result[i].triggerTime.time)).format("YYYY-MM-DD HH:mm:ss"):"")
					+ "</label></td><td><label >"
					+ (res.result[i].triggerCode==200?"<span style=\"color: green\">成功</span>":((res.result[i].triggerCode==500)?"<span style=\"color: red\">失败</span>":((res.result[i].triggerCode==0)?"":res.result[i].triggerCode)))
					+ "</label></td><td><label >"
					+ (res.result[i].triggerMsg?"<a class=\"logTips\" href=\"javascript:;\" style=\"color: #3c8dbc\">查看<span style=\"display:none;\">"+ res.result[i].triggerMsg +"</span></a>":"无")
					+ "</label></td><td><label >"
					+ res.result[i].executorAddress
					+ "</label></td><td><label >"
					+ (res.result[i].executorHandler==""?"GLUE模式":res.result[i].executorHandler)
					+ "</label></td><td><label >"
					+ res.result[i].executorParam
					+ "</label></td><td><label >"
					+ (res.result[i].handleTime?moment(new Date(res.result[i].handleTime.time)).format("YYYY-MM-DD HH:mm:ss"):"")
					+ "</label></td><td><label >"
					+ (res.result[i].handleCode==200?"<span style=\"color: green\">成功</span>":((res.result[i].handleCode==500)?"<span style=\"color: red\">失败</span>":((res.result[i].handleCode==0)?"":res.result[i].handleCode)))
					+ "</label></td><td><label >"
					+ (res.result[i].handleMsg?"<a class=\"logTips\" href=\"javascript:;\" >查看<span style=\"display:none;\">"+ res.result[i].handleMsg +"</span></a>":"无")
					+ "</label></td><td><label >";
					if(res.result[i].triggerCode==200){
						str+="<button type=\"button\" class=\"btn btn-blue btn-xs\" onclick=\"logdetail("+res.result[i].id+")\">执行日志</button>";
						if(res.result[i].handleCode==0){
							str+="<button type=\"button\" class=\"btn btn-blue btn-xs\" onclick=\"logkill("+res.result[i].id+")\">终止任务</button>";
						}
					}
					str+= "</label ></td></tr>";
		}
		$("#newspaperls").append(str);
        document.getElementById('pageSize').innerHTML=res.listsize;
		laypage({
			cont : document.getElementById('page1'), //容器。值支持id名、原生dom对象，jquery对象,
			pages : res.pageSize, //总页数
			skip : true, //是否开启跳页
			skin : 'molv', //加载内置皮肤，也可以直接赋值16进制颜色值，如:#c00
			curr : curr || 1,
			last: res.pageSize,
			groups : 6,//连续显示分页数
			jump : function(obj, first) { //触发分页后的回调
				if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
					list1(obj.curr);
					var curr = obj.curr;
					curr = obj.curr;
				}
			}
		});
	});
}
//获取执行器和任务
function init(){
	$.ajax({
		type : "post",
		url : "./xxljob_log_init.do",
		data : {
			"id":"0",
		},
		dataType : "json",
		async : false,
		success : function(data) {
			var jobinfoHtml="";
			var jobgrouphtml="";
			jobgrouphtml += "<option value=\"0\" >请选择</option>";
			for(var i=0;i<data.jobGroupsize;i++){
				if(jobgroupiid==data.jobGroup[i].id){
					jobgrouphtml += "<option value=\""+data.jobGroup[i].id+"\" selected=\"selected\">"+data.jobGroup[i].title+"</option>";
					getJobsByGroup(data.jobGroup[i].id);
				}else{
					jobgrouphtml += "<option value=\""+data.jobGroup[i].id+"\">"+data.jobGroup[i].title+"</option>";
				}
			}
			if(jobgroupiid==-1){
				jobinfoHtml += "<option value=\"0\" >请选择</option>";
			}
			
			$("#jobGroup").append(jobgrouphtml);
			$("#jobId").append(jobinfoHtml);
		},
		error : function(data) {
			layer.msg("请求失败！", {icon: 2});
		}
	});
	$("#jobGroup").on("change", function () {
		$("#jobId option").remove();
		var jobGroup = $(this).children('option:selected').val();
		$.ajax({
			type : 'POST',
            async: false,   // async, avoid js invoke pagelist before jobId data init
			url : './xxljob_log_getJobsByGroup.do',
			data : {"jobGroup":jobGroup},
			dataType : "json",
			success : function(data){
				//alert(data.code)
				var jobinfoHtml="";
				jobinfoHtml+="<option value=\"0\" >请选择</option>";
				$.each(data, function (n, value) {
					if(jobinfoiid==value.id){
						jobinfoHtml+="<option value=\""+value.id+"\"  selected=\"selected\">"+value.jobDesc+"</option>";
					}else{
						jobinfoHtml+="<option value=\""+value.id+"\" >"+value.jobDesc+"</option>";
					}
                   });
				$("#jobId").append(jobinfoHtml);
			},
		});
	});
	function getJobsByGroup(gid){
	//$("#jobGroup").on("change", function () {
		$("#jobId option").remove();
		//var jobGroup = $(this).children('option:selected').val();
		var jobGroup = gid;
		$.ajax({
			type : 'POST',
            async: false,   // async, avoid js invoke pagelist before jobId data init
			url : './xxljob_log_getJobsByGroup.do',
			data : {"jobGroup":jobGroup},
			dataType : "json",
			success : function(data){
				//alert(data.code)
				var jobinfoHtml="";
				jobinfoHtml+="<option value=\"0\" >请选择</option>";
				$.each(data, function (n, value) {
					if(jobinfoiid==value.id){
						jobinfoHtml+="<option value=\""+value.id+"\"  selected=\"selected\">"+value.jobDesc+"</option>";
					}else{
						jobinfoHtml+="<option value=\""+value.id+"\">"+value.jobDesc+"</option>";
					}
					
                   });
				$("#jobId").append(jobinfoHtml);
			},
		});
	}//);
}

//执行日志
function logdetail(logid){
	id=logid;
	layer.open({
		type : 2,
		area : [ '1350px', '700px' ],
		scrollbar : true,
		shadeClose : true,
		fix : true, //不固定
		title : "执行日志",
		//maxmin : true,  
		skin : 'layui-layer-rim', //加上边框
		btn : [ "关闭" ],
		content : [ "xxl-job-log-logdetail.html", "no" ],
		cancel : function(index) {
		}

	});
}

function logkill(logid){
	layer.alert('您确定要终止任务！！', {
    	skin: 'layui-layer-molv', //样式类名  自定义样式
   		closeBtn: 1,    // 是否显示关闭按钮
    	anim: 6, //动画类型
    	btn: ['确定','取消'], //按钮
    	icon: 2,    // icon
    	yes:function(){
        	$.ajax({
				type : "post",
				url : "./xxljob_log_logkill.do",
				data : {
					"id" : logid
				},
				dataType : "json",
				success : function(data) {
					if(data.result =="ok"){
						 var index = parent.layer.getFrameIndex(window.name); 
				         window.parent.get();
				         parent.layer.msg("终止成功!", {icon: 6});
				         parent.layer.close(index); 
					}else{
						layer.msg(data.result, {icon: 5});
					}
				},
				error : function(data) {
					layer.msg("请求失败!", {icon: 2});
				}
			});
   		}});
}
// 日志弹框提示
$('#newspaperls').on('click', '.logTips', function(){
	var msg = $(this).find('span').html();
	ComAlertTec.show(msg);
});
//日期空间
$(".reportrange").daterangepicker({
	timePicker: true, 			//是否显示小时和分钟
	timePickerIncrement: 10, 	//时间的增量，单位为分钟
	timePicker12Hour : false,	//是否使用12小时制来显示时间
	format: 'YYYY-MM-DD HH:mm:ss',
	separator : ' - ',
	ranges : {
        '最近1小时': [moment().subtract('hours',1), moment()],
        '今日': [moment().startOf('day'), moment()],
        '昨日': [moment().subtract('days', 1).startOf('day'), moment().subtract('days', 1).endOf('day')],
        '最近7日': [moment().subtract('days', 6), moment()],
        '最近30日': [moment().subtract('days', 29), moment()]
    },
    opens : 'left', //日期选择框的弹出位置
    format : 'YYYY-MM-DD HH:mm:ss',
    locale : {
    	customRangeLabel : '自定义',
        applyLabel : '确定',
        cancelLabel : '取消',
        fromLabel : '起始时间',
        toLabel : '结束时间',
        daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
        monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ],
        firstDay : 1
    }
    
},
function(start, end) {
    $('.reportrange span').html(start.format('YYYY-MM-DD HH:mm:ss') + ' - ' + end.format('YYYY-MM-DD HH:mm:ss'));
    //$('.reportrange span').html(moment().subtract('days', 29).format('YYYY-MM-DD 00:00:00') + ' - ' + moment().format('YYYY-MM-DD 00:00:00'));
    $('input[name="datestart"]').val(start.format('YYYY-MM-DD HH:mm:ss'));
    $('input[name="endstart"]').val(end.format('YYYY-MM-DD HH:mm:ss'));
});
//弹框日志
var ComAlertTec = {
    html:function(){
        var html =
            '<div class="modal fade" id="ComAlertTec" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
                '<div class="modal-dialog">' +
                    '<div class="modal-content-tec">' +
                        '<div class="modal-body"><div class="alert" style="color:#fff;"></div></div>' +
                        '<div class="modal-footer">' +
                            '<div class="text-center" >' +
                                '<button type="button" class="btn btn-green ok" data-dismiss="modal" >确认</button>' +
                            '</div>' +
                        '</div>' +
                    '</div>' +
                '</div>' +
            '</div>';
        return html;
    },
    show:function(msg, callback){
        // dom init
        if ($('#ComAlertTec').length == 0){
            $('body').append(ComAlertTec.html());
        }

        // 弹框初始
        $('#ComAlertTec .alert').html(msg);
        $('#ComAlertTec').modal('show');
        $('#ComAlertTec .ok').click(function(){
            $('#ComAlertTec').modal('hide');
            if(typeof callback == 'function') {
                callback();
            }
        });
    }
};

</script>